﻿/*
    Project: IV:MP GUI Editor (http://code.google.com/p/ivmp-gui-editor)
    Code license: GNU Lesser GPL (http://www.gnu.org/copyleft/lesser.html) 
 */

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
namespace IvmpEditor
{
    public class GUIEditBox : GUIElement
    {
        public new const string TYPE = "GUIEditBox";
        public override string GetTypeName() { return TYPE; }
        public override string GetDefaultVarName() { return "editBox"; }
        private new TextBox nativeControl;
        public new TextBox NativeControl { get { return this.nativeControl; } }
        public bool Multiline { get; set; }

        public GUIEditBox(string _variableName)
        {
            variableName = _variableName;
            properties = new Dictionary<string, object>(25);
            this.Multiline = false;
            MakeControl();
        }
        protected override void MakeControl()
        {            
            // Create a stylized native control of button
            nativeControl = new TextBox();
            base.nativeControl = this.nativeControl;
            moveControlAgent = new WpfMoveControlAgent(nativeControl);

            nativeControl.Style = GetBaseStyle();
            SetControlFont(nativeControl, "#tahoma-bold", 12, FontWeights.Black);
            nativeControl.MaxLines = 1;
            nativeControl.Text = variableName;
        }
        public override void SetText(string newTxt, bool native)
        {
            if (!this.Multiline && newTxt.Contains(Environment.NewLine))
                newTxt = newTxt.Substring(0, newTxt.IndexOf(Environment.NewLine));
            base.SetText(newTxt, native);
            if (native && nativeControl != null)
                nativeControl.Text = newTxt;
        }

        public override void SyncFromNative()
        {
            base.SyncFromNative();
            this.Multiline = nativeControl.MaxLines > 1;
        }
        public override void SyncToNative()
        {
            base.SyncToNative();
            nativeControl.MaxLines = this.Multiline ? Int32.MaxValue : 1;
        }
        public override string GetSQCode(ref SquirrelCodeStack sq)
        {
            sq.wSet(null, variableName, (this.Multiline ? "GUIMultiLineEditBox" : TYPE) + "()", true);

            WriteElementCommonCode(ref sq, new Point(1.85, -1.3), variableName);

            return variableName;
        }
        public override Style GetBaseStyle()
        {
            return App.Current.FindResource("CEGUI_EditBox") as Style;
        }
    }
}
